package com.gxa.community.admin.service.goods.impl;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.gxa.community.admin.mapper.goods.*;
import com.gxa.community.admin.service.goods.IGoodsInfoService;
import com.gxa.community.core.pojo.domain.goodsdo.*;
import com.gxa.community.core.pojo.vo.goodsvo.GoodsInfoVo;
import com.gxa.community.core.pojo.vo.goodsvo.GoodsVo;
import com.gxa.community.core.utils.R;
import lombok.extern.slf4j.Slf4j;
import lombok.var;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jms.core.JmsMessagingTemplate;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 商品信息表 服务实现类
 * </p>
 *
 * @author gxa
 * @since 2022-07-18
 */
@Service
@Slf4j
public class GoodsInfoServiceImpl extends ServiceImpl<GoodsInfoMapper, GoodsInfo> implements IGoodsInfoService {

   static Page<GoodsInfoVo> Page = new Page<>(1,5);
    @Autowired
    GoodsInfoVoMapper goodsInfoVoMapper;
    @Autowired
    GoodsClassMapper goodsClassMapper;
    @Autowired
    GoodsTagMapper goodsTagMapper;
    @Autowired
    GoodsBrandMapper goodsBrandMapper;
    @Autowired
    GoodsLogMapper goodsLogMapper;
    @Autowired
    GoodsVoMapper goodsVoMapper;
    @Resource(name = "MyjmsTemplate")
    private JmsMessagingTemplate jmsMessagingTemplate;
    private static final String GOODSDEl = "GoodsDel";
    /**
     * 新增商品
     * @param goodsInfo
     * @param name
     * @return
     */
    @Override
    public R add(GoodsInfo goodsInfo,Integer name) {
        //添加商品需要库存记录//需要添加事件
        boolean b = saveOrUpdate(goodsInfo);
        if (b){
            log.info("GoodsInfoServiceImpl:添加成功开始往记录表插入");
            //开始记录信息
            GoodsLog logs = new GoodsLog();
            logs.setGoodsNo(goodsInfo.getNos());//商品编号
            logs.setAdminId(name);    //操作人
            logs.setActionTime(System.currentTimeMillis()); //当前时间戳
            logs.setActionNum(goodsInfo.getNum());//操作数量
            logs.setStockNum(goodsInfo.getNum());//剩余库存
            logs.setActionType(1); // 操作类型1生成订单2取消订单3编辑商品
            logs.setType(2);//记录类型 1商品出库2商品入库
            //发消息删除所有关于商品的信息
            jmsMessagingTemplate.convertAndSend(GOODSDEl,new ArrayList<String>());
            int insert = goodsLogMapper.insert(logs);
            if (insert==0){
                log.error("添加商品操作:记录失败");
            }
            return R.ok();
        }
        return R.error();
    }

    /**
     * 修改商品并记录商品
     * @param goodsInfo
     * @param nameId
     * @return
     */
    @Override
    public R edit(GoodsInfo goodsInfo, Integer nameId) {
        int cont = 0;
        int a = 0;
        GoodsInfo goodsInfo01 = getById(goodsInfo.getId());
        if (goodsInfo01==null){
            return R.error("修改失败");
        }
        boolean b = saveOrUpdate(goodsInfo);
        if (!b){
            return R.error("修改失败");
        }
        if (goodsInfo01.getNum()>goodsInfo.getNum()){
            //减少了库存
            cont = goodsInfo01.getNum()-goodsInfo.getNum();
            a = 1;
        }
        if (goodsInfo01.getNum()<goodsInfo.getNum()){
            //增加了库存
            cont = goodsInfo.getNum()-goodsInfo01.getNum();
            a = 2;
        }
        if (goodsInfo01.getNum()==goodsInfo.getNum()){
            //没有修改库存
        }
        jmsMessagingTemplate.convertAndSend(GOODSDEl,new ArrayList<String>());
        log.info("GoodsInfoServiceImpl:修改成功开始往记录表插入");
        //开始记录信息
        GoodsLog logs = new GoodsLog();
        logs.setGoodsNo(goodsInfo.getNos());//商品编号
        logs.setAdminId(nameId);    //操作人
        logs.setActionTime(System.currentTimeMillis()); //当前时间戳
        logs.setActionNum(cont);//操作数量
        logs.setStockNum(goodsInfo.getNum());//剩余库存
        logs.setActionType(3); // 操作类型1生成订单2取消订单3编辑商品
        logs.setType(a);//记录类型 1商品出库2商品入库
        int insert = goodsLogMapper.insert(logs);
        if (insert==0){
            log.error("修改商品操作:记录失败");
        }
        return R.ok();
    }

    /**
     * 根据条件搜索
     * @param name 商品名称
     * @param tag  商品标签
     * @param classs 商品分类
     * @param max 商品库存(大)
     * @param mini 商品库存(小)
     * @return R
     */
    @Override
    public R seek(String name, String tag, String classs, String max, String mini,String status,Integer page,Integer size) {
        Page.setCurrent(page);
        Page.setSize(size);
        QueryWrapper<GoodsInfoVo> wrapper = new QueryWrapper<>();
        int cont = 0;
        if (StringUtils.isNotBlank(name)){
            wrapper.likeRight("name",name);
            cont++;
        }
        if (StringUtils.isNotBlank(tag)){
            wrapper.eq("tag_id",tag);
            cont++;
        }
        if (StringUtils.isNotBlank(classs)){
            wrapper.eq("classs_id",classs);
            cont++;
        }
        if (StringUtils.isNotBlank(status)){
            wrapper.eq("status",status);
            cont++;
        }
        if (mini=="0"&&StringUtils.isNotBlank(max)){
            wrapper.eq("num",mini);
            cont++;
        }
        if (max=="200"&&StringUtils.isNotBlank(mini)){
            wrapper.gt("num",max);
            cont++;
        }
        if (StringUtils.isNotBlank(mini)&&StringUtils.isNotBlank(max)){
            wrapper.between("num",mini,max);
            cont++;
        }
        log.info("搜索条件一共有:{}",cont);
        if (cont==0){
            var goodsInfoVoPage = goodsInfoVoMapper.selectPage(Page, null);
            return R.ok().put("data",goodsInfoVoPage.getRecords()).put("cont",goodsInfoVoPage.getTotal());
        }
        var goodsInfoVoPage = goodsInfoVoMapper.selectPage(Page, wrapper);
        return R.ok().put("data",goodsInfoVoPage.getRecords()).put("cont",goodsInfoVoPage.getTotal());
    }



    /**
     * 修改商品获取
     * @param id 修改商品的ID
     * @return R
     */
    @Override
    public R aloneQuery(Integer id) {
        GoodsInfo byId = getById(id);
        if (byId==null){
            return R.error("没有该用户");
        }
        if (byId.getStatus()==1){
            return R.error("商品上线中,请下线后编辑");
        }
        return R.ok().put("data",byId);
    }



    /**
     * 获取商品/通过视图查询  GoodsInfoVo对象查询
     * @param page 分页
     * @param size 分页
     * @return R
     */
    @Override
    public R query(Integer type,Integer page, Integer size) {
        if (page!=null&&size!=null){
            Page.setCurrent(page);
            Page.setSize(size);
        }
        QueryWrapper wrapper = new QueryWrapper<>();
        wrapper.eq("status",type);
        var page1 = goodsInfoVoMapper.selectPage(Page, wrapper);
        return R.ok().put("data",page1.getRecords()).put("cont",page1.getTotal());
    }

    /**
     * 批量删除商品
     * @param id 数组
     * @return R
     */
    @Override
    public R del(Integer... id) {
        jmsMessagingTemplate.convertAndSend(GOODSDEl,new ArrayList<String>());
        int cont = 0;
        for (Integer integer : id) {
            boolean b = removeById(integer);
            if (b){
                cont++;
            }
        }
        return R.ok("成功删除了"+cont+"条");
    }

    /**
     * 查询所有未禁用的添加属性
     * @return R
     */
    @Override
    public R classTag() {
        QueryWrapper wrapper = new QueryWrapper<>();
        wrapper.eq("status",1);
        List<GoodsBrand> goodsBrands = goodsBrandMapper.selectList(wrapper);
        List<GoodsClass> goodsClasses = goodsClassMapper.selectList(wrapper);
        List<GoodsTag> goodsTags = goodsTagMapper.selectList(wrapper);
        return R.ok().put("class",goodsClasses).put("tag",goodsTags).put("brands",goodsBrands);
    }

    /**
     * 远程通信接口
     * 根据分类获和标签获取商品
     * @return R
     */
    @Override
    public R queryClassGoodsList() {
        QueryWrapper <GoodsVo> wrapper = new QueryWrapper<>();
        wrapper.eq("status",1);
        List<GoodsVo> list = goodsVoMapper.selectList(wrapper);
        return R.ok().put("data",list);
    }


}
